package com.yantong.service.impl;

import com.yantong.pojo.Activity;
import com.yantong.mapper.ActivityMapper;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.Order;
import com.yantong.pojo.OrderProduct;
import com.yantong.mapper.OrderProductMapper;
import com.yantong.pojo.MemberProductAuthority;
import com.yantong.pojo.ProductAuthority;
import com.yantong.mapper.MemberProductAuthorityMapper;
import com.yantong.mapper.ProductAuthorityMapper;
import com.yantong.service.ProductAuthorityService;
import com.yantong.pojo.Member;
import com.yantong.mapper.MemberMapper;
import com.yantong.utils.ValidateUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 *
 * @author huangshuai-728982210@qq.com
 * @date 2021/5/28 15:30
 */
@Service
public class ProductAuthorityServiceImpl implements ProductAuthorityService {

	@Autowired
	private ProductAuthorityMapper productAuthorityMapper;
	@Autowired
	private OrderProductMapper orderProductMapper;
	@Autowired
	private ActivityMapper activityMapper;
	@Autowired
	private MemberProductAuthorityMapper memberProductAuthorityMapper;
	@Autowired
	private MemberMapper memberMapper;

	@Override
	public int saveCentreIndustry(ProductAuthority productAuthority) {
		return productAuthorityMapper.insertSelective(productAuthority);
	}

	@Override
	public PageUtils<ProductAuthority> listProductAuthority(ProductAuthority productAuthority, int offset, int limit) {
		PageUtils<ProductAuthority> pageUtils = new PageUtils<>();
		int total = productAuthorityMapper.countByAll(productAuthority);
		if (total > 0) {
			pageUtils.setTotal(total);
			pageUtils.setRows(productAuthorityMapper.selectByAll(productAuthority,new RowBounds(offset, limit)));
		}
		return pageUtils;
	}

	@Override
	public int updateProductAuthority(ProductAuthority productAuthority) {
		return productAuthorityMapper.updateByPrimaryKeySelective(productAuthority);
	}

	@Override
	public ProductAuthority getProductAuthorityById(Long id) {
		return productAuthorityMapper.selectByPrimaryKey(id);
	}

	@Override
	public int removeProductAuthority(Long id) {
		if (ValidateUtil.isEmptyObjectOrString(id)) {
			return 0;
		}
		return productAuthorityMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<ProductAuthority> listByAll() {
		return productAuthorityMapper.listByAll();
	}

	@Override
	public void MemberProductAuthoityUpdate(Order order) {
		Member member = memberMapper.selectByPrimaryKey(order.getMid());
		List<OrderProduct> orderProductList = orderProductMapper.getByOrderId(order.getId());
		Activity activity = activityMapper.selectByPrimaryKey(orderProductList.get(0).getProductId());
		//新增赠送的商品购买权限
		MemberProductAuthority memberProductAuthoritySend = new MemberProductAuthority();
		//查询该用户是否有该购买权限
		//if(activity.getShopProductAuthorityId() != null) {
		MemberProductAuthority memberProductAuthority = memberProductAuthorityMapper.getByProductAuthorityIdAndMid(activity.getShopProductAuthorityId(), member.getId());
            if(memberProductAuthority != null){
	            int countUserd = memberProductAuthority.getCountUsed();
	            memberProductAuthority.setCountUsed(countUserd + 1);
	            if (memberProductAuthority.getCount().equals(memberProductAuthority.getCountUsed())) {
		            memberProductAuthority.setStatus("0");
	            }
	            memberProductAuthorityMapper.updateByPrimaryKeySelective(memberProductAuthority);
            }

			ProductAuthority productAuthority = productAuthorityMapper.selectByPrimaryKey(activity.getHandselProductAuthorityId());
			if (productAuthority != null) {
				//查询用户是否已拥有了该赠送的权限
				MemberProductAuthority memberProductAuthorityOld = memberProductAuthorityMapper.getByProductAuthorityIdAndMid(activity.getHandselProductAuthorityId(), member.getId());
				if (memberProductAuthorityOld == null) {
					memberProductAuthoritySend.setMid(member.getId());
					memberProductAuthoritySend.setProductAuthorityId(productAuthority.getId());
					memberProductAuthoritySend.setCount(productAuthority.getCount());
					memberProductAuthoritySend.setCentreId(member.getCentreId());
					memberProductAuthorityMapper.insertSelective(memberProductAuthoritySend);
				}
			}
//		}
	}
}
